package com.caochenlei.graph;

import java.util.Objects;

public class DirectedEdge implements Comparable<DirectedEdge> {
    private final int v;                //当前边的起点
    private final int w;                //当前边的终点
    private final double weight;        //当前边的权重

    public DirectedEdge(int v, int w, double weight) {
        this.v = v;                     //初始化起点
        this.w = w;                     //初始化终点
        this.weight = weight;           //初始化权重
    }

    //获取有向边的起点
    public int from() {
        return v;
    }

    //获取有向边的终点
    public int to() {
        return w;
    }

    //获取有向边的权重
    public double weight() {
        return weight;
    }

    @Override
    public int compareTo(DirectedEdge that) {
        if (this.weight() > that.weight()) {
            //如果当前边的权重值比that边的权重值大，返回+1
            return +1;
        } else if (this.weight() < that.weight()) {
            //如果当前边的权重值比that边的权重值小，返回-1
            return -1;
        } else {
            //如果当前边的权重值和that边的权重值一样，返回0
            return 0;
        }
    }

    @Override
    public String toString() {
        return String.format("%d->%d %.2f", v, w, weight);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DirectedEdge that = (DirectedEdge) o;
        return v == that.v && w == that.w && Double.compare(that.weight, weight) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(v, w, weight);
    }
}
